package com.zeta.eduservice.service.impl;

import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduStatisticsDaily;
import com.zeta.eduservice.mapper.EduStatisticsDailyMapper;
import com.zeta.eduservice.service.EduStatisticsDailyService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.server.ServerEndpoint;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 网站统计日数据 服务类
 * </p>
 *
 * @author zeta
 * @since 2020-08-18
 */
@Service
public  class EduStatisticsDailyServiceImpl  extends ServiceImpl<EduStatisticsDailyMapper	, EduStatisticsDaily> implements EduStatisticsDailyService{

//	@Autowired
//	private UcenterClient ucenterClient;
//
	 @Override
	    public void registerCount(String day) {

	        //添加记录之前删除表相同日期的数据
	        QueryWrapper<EduStatisticsDaily> wrapper = new QueryWrapper<>();
	        wrapper.eq("date_calculated",day);
	        baseMapper.delete(wrapper);

	        //远程调用得到某一天注册人数
	       // R registerR = ucenterClient.countRegister(day);
//	        Integer countRegister = (Integer)registerR.getData().get("countRegister");

	        //把获取数据添加数据库，统计分析表里面
//	        EduStatisticsDaily sta = new EduStatisticsDaily();
//	        sta.setRegisterNum(countRegister); //注册人数
//	        sta.setDateCalculated(day);//统计日期
//
//	        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
//	        sta.setLoginNum(RandomUtils.nextInt(100,200));
//	        sta.setCourseNum(RandomUtils.nextInt(100,200));
//	        baseMapper.insert(sta);
	    }

	    //图表显示，返回两部分数据，日期json数组，数量json数组
	    @Override
	    public Map<String, Object> getShowData(String type, String begin, String end) {
	        //根据条件查询对应数据
	        QueryWrapper<EduStatisticsDaily> wrapper = new QueryWrapper<>();
	        wrapper.between("date_calculated",begin,end);
	        wrapper.select("date_calculated",type);
	        List<EduStatisticsDaily> staList = baseMapper.selectList(wrapper);

	        //因为返回有两部分数据：日期 和 日期对应数量
	        //前端要求数组json结构，对应后端java代码是list集合
	        //创建两个list集合，一个日期list，一个数量list
	        List<String> date_calculatedList = new ArrayList<>();
	        List<Integer> numDataList = new ArrayList<>();

	        //遍历查询所有数据list集合，进行封装
	        for (int i = 0; i < staList.size(); i++) {
	        	EduStatisticsDaily daily = staList.get(i);
	            //封装日期list集合
	            date_calculatedList.add(daily.getDateCalculated());
	            //封装对应数量
	            switch (type) {
	                case "login_num":
	                    numDataList.add(daily.getLoginNum());
	                    break;
	                case "register_num":
	                    numDataList.add(daily.getRegisterNum());
	                    break;
	                case "video_view_num":
	                    numDataList.add(daily.getVideoViewNum());
	                    break;
	                case "course_num":
	                    numDataList.add(daily.getCourseNum());
	                    break;
	                default:
	                     break;
	            }
	        }
	        //把封装之后两个list集合放到map集合，进行返回
	        Map<String, Object> map = new HashMap<>();
	        map.put("date_calculatedList",date_calculatedList);
	        map.put("numDataList",numDataList);
	        return map;
	    }

	
}




